#
# pip3 install -i https://pypi.doubanio.com/simple paho-mqtt
# meeting.xmgkfw.com:42023
# admin/Hhuc1115 
# "42023:18083" #emqx-dashboard
# "42024:1883"  #emqx-http
# "42025:8081"  #emqx-https
# "42026:8083"  #emqx-ws 
# "42027:8084"  #emqx-wss
#
print('-- load mq nodule --')
from pickle import NONE
from paho.mqtt import client as mqtt_client
import random
print('-- load mq nodule success.--')

class TMqClientEvent:
    def __init__(self , _owner ) -> None:
        self.owner = _owner
    def onConnected(self):     
        try:
            pass
        except Exception as e:
            print(e)
    def onConnectedFaild(self):     
        try:
            pass
        except Exception as e:
            print(e)
    def onDisConnected(self):     
        try:
            pass
        except Exception as e:
            print(e)
    def onRecMsg(self):     
        try:
            pass
        except Exception as e:
            print(e)
        
class TMqClient:
    def __init__(self) -> None:
        self.broker =""
        self.port=3884
        self.topic="" 
        self.events = TMqClientEvent( self )
        self.mqClient = None
        self.clientId=""
    def getChielId(self):
        client_id = f'mcid{random.randint(0, 10000)}{random.randint(0, 10000)}{random.randint(0, 10000)}'
        return client_id
    def isConnected(self):
        ret = self.mqClient!=None and self.mqClient.is_connected()
        return ret
    def open(self):        
        try:
            clientId = self.getChielId()
            client = mqtt_client.Client(clientId)
            client.on_connect = self.onconn
            client.on_disconnect = self.ondisconn
            client.on_log = self.onlog
            client.on_connect_fail = self.onmessage
            client.connect( self.broker , self.port)
            client.loop_forever()
        except Exception as e:
            print(e)
    def onconn(self, client, userdata, flags, rc):
        print('connect success')
        try:
            if( self.events.onConnected and rc==0):
                self.events.onConnected()
            else:
                self.events.onConnectedFaild()
        except Exception as er :
            print(er)
    def ondisconn(self , client, userdata, flags, rc):
        print('disconnect')
    def onlog(self,client, userdata, level, buf):
        print('connect faild')
    def onmessage(self,client, userdata, message):
        print('connect message.')
            
    def onConnect (self , client, userdata, flags, rc)->None:
        if rc==0:
            print('connect to broker.')
            if self.events.onConnect!=None:
                try:
                    self.events.onConnect( )
                except Exception as e :
                    print(e)
        else:
            print('connect to broker faild.')
            if self.events.onConnectedFaild!=None:
                try:
                    self.events.onConnectedFaild( )
                except Exception as e :
                    print(e)
        
    def close(self):
        try:
            pass
        except Exception as e:
            print(e)
    def publishData(self , data , topic) :
        try:
            pass
        except Exception as e:
            print(e)
    def subscribe(self , topic) :
        try:
            pass
        except Exception as e:
            print(e)
    

###
#mq = TMqClient()
#mq.broker = "meeting.xmgkfw.com"
#mq.port = 42024
#mq.open()
###
